<?php
/*
 * @project CreativeFramework
 * @version <#RELEASE_VERSION#>
 * @license GNU Lesser General Public License
 * @author Jimmie Lin
 */

class Dashboard extends CF_Controller {
	function __construct() {
		global $Lang, $UI;
		$this->title = "CP Dashboard";
		$this->desc = "";
		$UI->write_sidebar_head("Actions");
		$UI->write_sidebar_link("/admin.php?app=global&controller=dashboard", "Dashboard", "silk/tag_blue.png");
		$UI->write_sidebar_link("/admin.php?app=global&controller=dashboard&act=credits", "Credits", "silk/page_white_code.png");
		$UI->write_sidebar_link("/admin.php?app=global&controller=dashboard&act=iinfo", "Install Info", "silk/error.png");
		// Continue,
		$UI->write_sidebar_head("Quick Links");
		$UI->write_sidebar_link("/admin.php?app=global&controller=config", "Configure CF", "silk/cog.png");
		$UI->write_sidebar_link("/index.php?app=global&controller=logout", "Log Out", "silk/delete.png");
	}
	
	function Iinfo() {
		global $UI, $Views, $Smarty;
		$UI->message("<b>(In progress)</b> Planned for Technical Preview 2 (1/20/2010)", "error");
	}
	
	function Credits() {
		/*
			Note: This is not supposed to follow the default cf Hybrid Coding standards.
			This function was written just to make it simple.
			
			Therefore, this page is *not* an example of how you do simple pages in CF.
			
			FIXME: Make this to follow standards. kay?
		*/
		global $Views, $UI;
		$UI->message("The hall of fame: Who wrote CF? Who made it possible? Well, everything about this is here.", "info");
		$Views->Load("credits");
	}
	
	function Index() {
		global $Views, $Models, $DB, $UI, $Smarty, $cURL, $Main;
		$UI->message("This is your Control Panel Dashboard - It gives you a quick overview of your installation.", "info");
		$UI->message("To begin using the CP, use the left menu pane and the top tab menu.", "info");
		
		/*
		 * Column 1: Statistics + ACP Login Logs
		 */
		$Users_model = $Models->Load("users_model");
		// Number of users,
		$N = $Users_model->retrieve("all");
		$Data = count($N);
		$Smarty->assign("numusers", $Data);
		// Latest user,
		$LatestUser = $Users_model->retrieve("last", 1);
		$Smarty->assign("lastuser", $LatestUser);
		// Build the template and we're done here
		$Built_Stats = $Smarty->fetch("global/admin/dashboard_stats.tpl");
		
		// ACP Login Logs...
		$Login_log = "";
		$LoginLogs = $DB->Driver->simple_select("acp_logins", "*", "", array(array("limit", "5"), array("order", "time", "DESC")));
		while($LoginLog = $DB->Driver->fetch_array($LoginLogs)) {
			$Smarty->assign("LoginLog", $LoginLog);
			// Get user, but first
			if(!isset($this->userarray[$LoginLog["uid"]])) {
				// OK, get it
				$this->userarray[$LoginLog["uid"]] = $DB->Driver->fetch_array($DB->Driver->simple_select("users", "*", "`id` = '{$LoginLog['uid']}'"));
			}
			// Send it to smarty
			$Smarty->assign("LoginLogUser", $this->userarray[$LoginLog["uid"]]);
			$Login_log .= $Smarty->fetch("global/admin/dashboard_login_log.tpl");
		}
		
		/*
		 * Column 2: Update Manager + Version History
		 */
		 
		// Update Manager
		
		$Check = check_updated(); // UMng Wrapper
		if(!$Check)
			$Update_manager = $UI->message("Warning: Your CF Installation is not up-to-date!", "error", true);
		elseif($Check == 2)
			$Update_manager = $UI->message("You're running a development version - keep an eye on BUGS! AAAH!!", "warning", true);
		else
			$Update_manager = $UI->message("Your CF Installation is Up-To-Date. Keep an eye on this section!", "success", true);
		$Update_manager .= $UI->message("You can get updates from the <a href='http://creativebb.net'>Official CF Website</a> when available.", "regular", true);
		$Update_manager = $UI->basicTable("Update Manager", $Update_manager, "silk/transmit.png");
		
		// Version History
		
		$V_History = $DB->Driver->simple_select("upgrade_history", "*", "", array("limit" => 5));
		$Data = "";
		while($a = $DB->Driver->fetch_array($V_History)) {
			$Smarty->assign("a", $a);
			$Data .= $Smarty->fetch("global/admin/dashboard_upg_history.tpl");
		}
		$Update_manager .= $UI->basicTable("Upgrade History", $Data, "silk/arrow_refresh.png", false);
		
		//------------------------------------
		// End
		
		$UI->twoColumnLayout(
			"50%",
			"50%",
				$UI->basicTable("Statistics", $Built_Stats, "silk/chart_line.png", false).
				$UI->basicTable("Last 5 ACP Logins", $Login_log, "silk/user_gray.png", false),
			$Update_manager
		);
		/*
		 * Some more technical stuff, admin notepad, bla, bla bla
		 */
		// Stats (Left)
		$Tech_Stats = $Smarty->fetch("global/admin/dashboard_tech_stats.tpl");
		// Admin Notepad (Right)
		// Did we get an admin notepad update?
		if(isset($_POST['admin_notepad'])) {
			global $IN;
			$Note_Content = $Main->update_config_value("admin_notepad", $IN->strip_html($_POST['admin_notepad']));
			$Notepad = $UI->message("Admin Notepad updated!", "success", true);
		}
		else $Notepad = "";
		// Get it,
		$Smarty->assign("notepad_content", $Main->config_value("admin_notepad", true));
		$Notepad .= $Smarty->fetch("global/admin/dashboard_notepad.tpl");
		$UI->twoColumnLayout(
			"50%",
			"50%",
			$UI->basicTable("Technical Information", $Tech_Stats, "silk/wrench.png", false),
			$UI->basicTable("Admin Notepad", $Notepad, "silk/pencil.png")
		);
	}
}
